// pages/me/manageTeam/manageTeam.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        StatusBar: app.globalData.StatusBar,
        CustomBar: app.globalData.CustomBar,
        requestParam:{
            queryType: 0,
            keyword: ''
        },
        modalName: null,
        teamInfo:[],
        applyList:[],
        roleList:[
            {value:'viewCar',name:'查看车辆'},
            {value:'addCar',name:'发布车辆'},
            {value:'editCar',name:'编辑车辆'},
            {value:'closCar',name:'下架车辆'},
            {value:'manageWantBuy',name:'管理求购'},
            {value:'manageAuction',name:'管理拍卖'},
            {value:'manageSecondBatch',name:'管理一口价'},
            {value:'contactCustomer',name:'联系客户'},
            {value:'manageDealer',name:'管理公司'},
            {value:'manageTeam',name:'管理团队'},
            {value:'amountManage',name:'账户管理'},
            {value:'reportApplyAudit',name:'报告审批'}
        ],
        showRoleName:{},
    },

    /**
     * 生命周期函数--监听页面加载
     */
    doNothing(){},
    onShow: function (options) {
        this.InitUserStatus();
    },
    // 检查一下登录状态。
    async InitUserStatus(){
        app.showLoading("获取用户信息");
        await app.checkToken();
        this.InitUserInfo();
        wx.hideLoading();
    },
    InitUserInfo(){
        let userInfo = app.globalData.userInfo;
        // 这里再验证一遍权限。
        if (userInfo.roleList.indexOf('manageTeam') == -1) {
            app.showNoAuthOrNoGrant('您无权操作查看此页面');
            return;
        }else{
            this.loadTeamInfo();
            this.dealerUserApplyList();
        }
    },
    // 获取团队成员
    loadTeamInfo(){
        app.sHttp.dealerUserList(this.data.requestParam).then(res =>{
            let showRoleName = {};
            this.data.roleList.forEach(item =>{
                showRoleName[item.value] = item.name;
            });
            res.userList.forEach((item,index) =>{
                item.roleObj = {'viewCar':0,'addCar':0,'editCar':0,'closCar':0,'manageWantBuy':0,'manageAuction':0,'manageSecondBatch':0,'contactCustomer':0,'manageDealer':0,'manageTeam':0};
                item.roleList.forEach(ele =>{
                    item.roleObj[ele] = 1;
                })
            })
            this.setData({
                'teamInfo': res.userList,
                'showRoleName': showRoleName
            })
        },err=>{
          app.error(err.msg);
        })
    },
    dealerUserAuditApply(e){
        let user = this.data.applyList[e.currentTarget.dataset.idx];
        user.roleList = [];
        user.auditStatus = e.currentTarget.dataset.type;
        if(e.currentTarget.dataset.type == 1){
            if(undefined == user.userTitle || null == user.userTitle || "" == user.userTitle){
                app.alert('请输入职务信息');
                return;
            }
        }
        app.sHttp.dealerUserAuditApply(user).then(res =>{
            this.dealerUserApplyList();
        },err=>{
          app.error(err.msg);
        })
    },
    inputZw(e){
        let index = e.currentTarget.dataset.idx;
        let applyList = this.data.applyList;
        applyList[index].userTitle = e.detail.value;
        this.setData({
            'applyList': applyList
        })
    },
    // 获取申请成员
    dealerUserApplyList(){
        app.sHttp.dealerUserApplyList().then(res =>{
            this.setData({
                'applyList': res.applyList
            })
        },err=>{
          app.error(err.msg);
        })
    },
    bindtapInput(e){
        this.setData({
            'requestParam.keyword' :e.detail.value
        })
    },
    switchType(e){
        let type = e.currentTarget.dataset.type;
        let _this = this;
        this.setData({
            'requestParam.queryType': Number(type)
        },(res)=>{
            _this.loadTeamInfo();
        })
    },
    changeUserRole(e){
        let uindex = e.currentTarget.dataset.uindex;
        let uid = e.currentTarget.dataset.uid;
        let roleList = e.detail.value;
        let teamInfo = this.data.teamInfo;
        if(uid == teamInfo[uindex].userId){
            teamInfo[uindex].roleList = roleList;
            teamInfo[uindex].roleObj = {'viewCar':0,'addCar':0,'editCar':0,'closCar':0,'manageWantBuy':0,'manageAuction':0,'manageSecondBatch':0,'contactCustomer':0,'manageDealer':0,'manageTeam':0};
            roleList.forEach(item =>{
                teamInfo[uindex].roleObj[item] = 1
            })
            this.setData({
                'teamInfo': teamInfo
            })
        };
    },
    // 禁用启用账户.
    modifyUserDisable(e){
        let user = e.currentTarget.dataset.user;
        delete user.roleObj;
        user.status == 1?user.status = 2:user.status =1;
        app.sHttp.dealerUserModify(user).then(res=>{
            app.alert(1 == user.status?'启用成功':'禁用成功');
            this.loadTeamInfo();
        },err=>{
          app.error(err.msg);
        });
    },
    saveUser(e){
        let user = app.deepCopy(this.data.teamInfo[e.currentTarget.dataset.index]);
        delete user.roleObj;
        app.sHttp.dealerUserModify(user).then(res=>{
            app.alert('保存成功');
        },err=>{
          app.error(err.msg);
        });

    },
    hideModal(){
        this.loadTeamInfo();
        this.setData({
            modalName :null
        })
    },
    showModal(e){
        if(this.data.applyList.length == 0){
            app.alert('暂无新申请');
            return;
        }
        this.setData({
            modalName :e.currentTarget.dataset.target
        })
    },
    jumpToDealerUserApply(){
        if(this.data.applyList.length == 0){
            app.alert('暂无新申请');
            return;
        }
        wx.navigateTo({
          url: '/pages/me/dealerUserApply/applyList/applyList',
        })
    },

    jumpToDealerUserEdit(e){
        app.globalData.ScopeUser = app.deepCopy(this.data.teamInfo[e.currentTarget.dataset.idx]);
        wx.navigateTo({
          url: '/pages/me/manageTeam/editTeamUser/editTeamUser?uid='+e.currentTarget.dataset.uid,
        })
    },

})